package net.cyue.web.easyquery.core.http.util;

import net.cyue.util.StringUtil;
import net.cyue.web.easyquery.core.http.data.QueryParameter;

public class ParameterValidator {

    public static class ValidationResult {
        private final boolean isValid;
        private final String message;

        public ValidationResult(boolean isValid, String message) {
            this.isValid = isValid;
            this.message = message;
        }

        public boolean isValid() {
            return isValid;
        }
        public String getMessage() {
            return message;
        }
    }

    public static ValidationResult validate(
        QueryParameter parameter,
        Object value
    ) {
        if (parameter.getName() == null) {
            throw new IllegalArgumentException("parameter name cannot be null");
        }
        if (parameter.getType() == null) {
            throw new IllegalArgumentException("parameter type cannot be null");
        }

        // 默认值
        if (value == null) {
            value = parameter.getDefaultValue();
        }

        if (value == null) {
            // 必要无值，总是错误的
            if (parameter.isRequired()) {
                return new ValidationResult(
                    false,
                    "parameter " + parameter.getName() + " is required"
                );
            }
            // 不必要无值，总是正确的
            else {
                return new ValidationResult(
                    true,
                    null
                );
            }
        }

        // 必要或不必要，总之有值，判断类型

        if (
            parameter.getType().equals(QueryParameter.Type.ANY) ||
            parameter.getType().equals(QueryParameter.Type.STR) ||
            parameter.getType().equals(QueryParameter.Type.STRING)
        ) {
            if (StringUtil.isBlank(value.toString())) {
                // 不必要为空值，正确的
                if (!parameter.isRequired()) {
                    return new ValidationResult(
                        true,
                        null
                    );
                }
                // 必要不可为空
                return new ValidationResult(
                    false,
                    "parameter " + parameter.getName() + " cannot be empty"
                );
            }
        } else if (
            parameter.getType().equals(QueryParameter.Type.INT) ||
            parameter.getType().equals(QueryParameter.Type.INTEGER)
        ) {
            try {
                Integer.parseInt(value.toString());
            } catch (NumberFormatException e) {
                // 类型不对总是错误的
                return new ValidationResult(
                    false,
                    "parameter " + parameter.getName() + " must be an integer"
                );
            }
        } else if (parameter.getType().equals(QueryParameter.Type.LONG)) {
            try {
                Long.parseLong(value.toString());
            } catch (NumberFormatException e) {
                // 类型不对总是错误的
                return new ValidationResult(
                    false,
                    "parameter " + parameter.getName() + " must be a long"
                );
            }
        } else if (
            parameter.getType().equals(QueryParameter.Type.FLOAT) ||
            parameter.getType().equals(QueryParameter.Type.DOUBLE) ||
            parameter.getType().equals(QueryParameter.Type.NUMBER)
        ) {
            try {
                Double.parseDouble(value.toString());
            } catch (NumberFormatException e) {
                // 类型不对总是错误的
                return new ValidationResult(
                    false,
                    "parameter " + parameter.getName() + " must be a number"
                );
            }
        }
        // boolean 不用判断

        return new ValidationResult(
            true,
            null
        );
    }
}
